package com.yunfei.api.v2.service;
import com.yunfei.api.v2.common.YunHeTongApiContant;
import com.yunfei.api.v2.param.CreatePersonInfoParam;
import com.yunfei.api.v2.param.LongTokenParam;
import com.yunfei.api.v2.param.getBase64.GetBase64Param;
import com.yunfei.api.v2.param.personMoulage.PersonMoulageParam;
import com.yunfei.api.v2.result.addAccountInfo.AddAccountInfoResultModel;
import com.yunfei.api.v2.result.createPersonInfo.CreatePersonInfoJsonRootBean;
import com.yunfei.api.v2.result.createPersonMoulage.CreatePersonMoulageData;
import com.yunfei.api.v2.result.createPersonMoulage.CreatePersonMoulageJsonRootBean;
import com.yunfei.api.v2.result.getBase64Moulage.GetBase64MoulageJson;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.ResultStatus;
import com.yunfei.utils.http.CreatePersonInfoHttpUtil;
import com.yunfei.utils.http.JsonParamHttpUtil;
import com.yunfei.utils.string.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* @Description:创建个人印章
* @Author: zhengyunfei
* @Date: 2019/8/9
* @time:10:48
*/
@Service
public class CreatePersonMoulageService {
    @Autowired
    private Environment env;
    @Autowired
    private GetLongTokenService getLongTokenService;
    @Autowired
    private QueryUserNoService queryUserNoService;
    /**
     * @Description:根据 moulageId 获取印章
     * @Author: zhengyunfei
     * @Date: 2019/8/15
     * @time:16:26
     */
    public ResultModel getBase64Moulage(GetBase64Param param,LongTokenParam longTokenParam){
        ResultModel resultModel=new ResultModel();
        try{
            String token=getLongTokenService.getLongToken(longTokenParam);
            JSONObject resultJson= JsonParamHttpUtil.get(YunHeTongApiContant.GET_BASE64_MOULAGE_API+param.getMoulageId(),token);
            GetBase64MoulageJson createPersonInfoJsonRootBean= (GetBase64MoulageJson) JSONObject.toBean(resultJson,GetBase64MoulageJson.class);
            JSONObject jsonObject=new JSONObject();
           System.out.println("获取印章返回结果==="+jsonObject.fromObject(createPersonInfoJsonRootBean));
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setData(createPersonInfoJsonRootBean);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultModel;
    }
    /**
    * @Description: 创建个人印章
    * @Author: zhengyunfei
    * @Date: 2019/8/15
    * @time:16:26
    */
    public ResultModel createPersonMoulage(PersonMoulageParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            LongTokenParam longTokenParam=new LongTokenParam();
            String token=getLongTokenService.getLongToken(longTokenParam);
            JSONObject jsonObject=JSONObject.fromObject(param);
            String jsonParam=jsonObject.toString();
            JSONObject resultJson=CreatePersonInfoHttpUtil.post(token, YunHeTongApiContant.CREATE_PERSION_MOULAGE_API,jsonParam);
            System.out.println("创建个人印章接口参数==========================="+jsonParam);
            CreatePersonMoulageJsonRootBean createPersonInfoJsonRootBean= (CreatePersonMoulageJsonRootBean) JSONObject.toBean(resultJson,CreatePersonMoulageJsonRootBean.class);
            int code=createPersonInfoJsonRootBean.getCode();
            if(code==200){
                resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
                resultModel.setCode(ResultStatus.SUCCESS.getCode());
                CreatePersonMoulageData createPersonMoulageData=new CreatePersonMoulageData();
                String moulageId=createPersonInfoJsonRootBean.getData().getMoulageId()+"";
                createPersonMoulageData.setMoulageId(moulageId);
                resultModel.setData(createPersonMoulageData);
            }else{
                //如果已经认证了，那么去查询一下
                resultModel.setMessage(createPersonInfoJsonRootBean.getMsg());
                resultModel.setCode(ResultStatus.SUCCESS.getCode());
                return resultModel;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultModel;
    }
    /**
     * @Description: 创建个人用户接口,返回签署者id
     * @param  CreatePersonInfoParam 创建个人信息参数
     * @param  token 如果已经获取了，就填，没有的填空后，接口自动调取，此结果返回签署者ID
     * @Author: zhengyunfei
     * @Date: 2019/8/15
     * @time:16:26
     */
    public String createPersonInfo(CreatePersonInfoParam param,String token){
       String signerId="";//签署者id
        Map<String,Object> queryMap=new HashMap<>();
        try{
            LongTokenParam longTokenParam=new LongTokenParam();
            if(StringUtil.isNullOrEmpty(token)){
                 token=getLongTokenService.getLongToken(longTokenParam);
            }
            JSONObject jsonObject=JSONObject.fromObject(param);
            String jsonParam=jsonObject.toString();
            JSONObject resultJson=CreatePersonInfoHttpUtil.post(token, YunHeTongApiContant.CREATE_PERSION_INFO_API,jsonParam);
            System.out.println("创建个人用户接口参数==========================="+jsonParam);
            CreatePersonInfoJsonRootBean createPersonInfoJsonRootBean= (CreatePersonInfoJsonRootBean) JSONObject.toBean(resultJson,CreatePersonInfoJsonRootBean.class);
            int code=createPersonInfoJsonRootBean.getCode();
            if(code==200){
                 signerId=createPersonInfoJsonRootBean.getData().getSignerId()+"";
            }else{
                //如果已经认证了，那么去查询一下
                 String certifyNum=param.getCertifyNum();
                signerId=queryUserNoService.queryUserNo(certifyNum,token);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return signerId;
    }

}
